package com.cj.bus.controller;


import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cj.bus.common.entity.ResponseEntity;
import com.cj.bus.common.entity.ResponseHelper;
import com.cj.bus.common.entity.tree.MultiTreeNode;
import com.cj.bus.common.entity.tree.SingleTreeNode;
import com.cj.bus.common.utils.ToMultiTreeTransformerUtil;
import com.cj.bus.entity.BusResourceTypeEntity;
import com.cj.bus.service.BusResourceTypeService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@RequestMapping("/resourceType")
@Validated
@Api(tags = {"资源类型相关接口"})
@Slf4j
public class BusResourceTypeController {

	@Autowired private BusResourceTypeService busResourceTypeService;
	
	@GetMapping("/list")
	@ApiOperation(value = "查询所有的资源树")
	public ResponseEntity<List<MultiTreeNode>> list() {
		
		List<BusResourceTypeEntity> list = busResourceTypeService.list(null);
		if(CollectionUtils.isEmpty(list)) {
			return ResponseHelper.success(new ArrayList<>());
		}
		List<SingleTreeNode> treesList = new ArrayList<>();
		
		for(BusResourceTypeEntity resourceType : list) {
			SingleTreeNode treeNode = new SingleTreeNode();
			treeNode.setId(resourceType.getTypeId().intValue());
            treeNode.setPId(resourceType.getParentId().intValue());
            treeNode.setText(resourceType.getTypeName());
            treesList.add(treeNode);
		}
		return ResponseHelper.success(ToMultiTreeTransformerUtil.transform(treesList));
	}


}
